package so.ifl.manager.admin;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.bstek.dorado.annotation.DataProvider;
import com.bstek.dorado.annotation.DataResolver;
import com.bstek.dorado.annotation.Expose;
import com.bstek.dorado.data.entity.EntityState;
import com.bstek.dorado.data.entity.EntityUtils;
import com.bstek.dorado.data.provider.Page;
import com.bstek.dorado.data.resolver.DataItems;
import com.bstek.dorado.web.DoradoContext;

import so.ifl.commons.tools.DateUtil;
import so.ifl.commons.tools.DoradoSystemUtils;
import so.ifl.commons.tools.except.SessionTimeOutException;
import so.ifl.servcie.ExamBookingService;
import so.ifl.vo.AccountVo;
import so.ifl.vo.ExamBookingDetailVo;
import so.ifl.vo.ExamBookingVo;
import so.ifl.vo.ExamHistoryVo;

/**
 * 
 * create by simon
 * create at 2012-1-17 下午11:37:34
 * <mailto:liangyuanliu@gmail.com>
 */
@Component("exambookingManager")
public class ExamBookingManager
{

	@Autowired
	private ExamBookingService exambookingService;
	
	private Logger logger_ = LoggerFactory.getLogger(this.getClass());
	
	
	/**
	 * 
	 * simon
	 * create at 2012-1-17 下午11:23:22
	 * <mailto:liangyuanliu@gmail.com>
	 * @param page
	 * @param map
	 */
	@DataProvider
	public void getExamBooking(Page<ExamBookingVo> page, Map<String, Object> map) {
		logger_.debug("params = " + map);

		int offset = page.getFirstEntityIndex();
		int limmit = page.getPageSize();
		if (map == null) {
			map = new HashMap<String, Object>();
		}else{
			Date less_time = (Date)map.get("less_time_");
			Date bigger_time = (Date)map.get("bigger_time");
			
			if (null != less_time && null != bigger_time) {
				try {
					map.put("less_time_", DateUtil.getFirstSecondInDay(less_time));
					map.put("bigger_time", DateUtil.getLastSecondInDay(bigger_time));
				} catch (Exception e) {
					logger_.error("提取查询参数失败：" + e.getMessage());
				}
				
			}
		}

		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		if (null != accountVo && null !=  accountVo.getAccoutId()) {
			map.put("company_id", accountVo.getCompanyId());
			
			
			List<ExamBookingVo> list = exambookingService.queryExamBookingWithParamAndPagination(map, offset, limmit);

			int countsql = exambookingService.countExamBookingWithParam(map);
			page.setEntities(list);
			page.setEntityCount(countsql);
		}else{
			logger_.error("登录超时。。");
			
			page.setEntities(new ArrayList<ExamBookingVo>());
			page.setEntityCount(0);
		}
		
		
	}
	
	/**
	 * 查询考试记录
	 * create by aaron.lu
	 * create at 2012-5-11 下午9:47:24
	 * <mailto: atyuan.lu@gmail.com>
	 * @param page
	 * @param map
	 *
	 */
	@DataProvider
	public void getExamHistoy(Page<ExamHistoryVo> page, Map<String, Object> map){
		logger_.debug("params = " + map);

		int offset = page.getFirstEntityIndex();
		int limmit = page.getPageSize();
		if (map == null) {
			map = new HashMap<String, Object>();
		}
		
		List<ExamHistoryVo> list = exambookingService.queryExamHistory(map, offset, limmit);

		int countsql = exambookingService.countExamHistory(map);
		page.setEntities(list);
		page.setEntityCount(countsql);
	}
	
	
	/**
	 * 回表
	 * create by aaron.lu
	 * create at 2012-3-24 下午11:58:44
	 * <mailto: atyuan.lu@gmail.com>
	 * @param examBookingId
	 *
	 */
	@Expose
	public Map<String, Object> returnBill(Integer examBookingId){
		Map<String, Object> re = new HashMap<String, Object>();
		
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		boolean flag = false;
		if (null != accountVo && null != accountVo.getAccoutId()) {
			flag = exambookingService.returnBill(examBookingId, accountVo);
		}
		
		re.put("success", flag);
		
		return re;
	}
	
	/**
	 * 一次保存考试预约
	 * create by aaron.lu
	 * create at 2012-3-4 下午11:59:17
	 * <mailto: atyuan.lu@gmail.com>
	 * @param ExamBookings
	 * @param ExamBookingdetails
	 * @throws Exception 
	 *
	 */
	@DataResolver
	public void saveExamBookingInfoOnce(DataItems dataItems) throws Exception{
		HashMap<String, Object> params = dataItems;
		
		
		ArrayList<ExamBookingVo> dataSetExamBooking = (ArrayList<ExamBookingVo>)params.get("dataSetExamBooking");
		ArrayList<ExamBookingDetailVo> dataSetExamBookingDetail = (ArrayList<ExamBookingDetailVo>)params.get("dataSetExamBookingDetail");
		
		logger_.debug("dataSetExamBooking = " + dataSetExamBooking);
		logger_.debug("dataSetExamBookingDetail = " + dataSetExamBookingDetail);
		
		if (null != dataSetExamBooking && dataSetExamBooking.size() > 0) {
			
			AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
			
			if (null != accountVo && null != accountVo.getAccoutId()) {
				for (int i = 0; i < dataSetExamBooking.size(); i++) {
					ExamBookingVo exambookingVo = dataSetExamBooking.get(i);
					exambookingVo.setCompanyId(accountVo.getCompanyId());
					exambookingVo.setCreateUser(accountVo.getAccountLoginName());
					exambookingVo.setCreateTime(new Date());
					//exambookingVo.setExamCreateUser(accountVo.getAccountOwner());
				}
			}else{
				throw new SessionTimeOutException("登录超时...");
			}
			
			
			
		}
		
		exambookingService.saveExamBookingInfo(dataSetExamBooking, dataSetExamBookingDetail);
		
	}
	

	@DataResolver
	public void updateExamBooking(Collection<ExamBookingVo> ExamBookings) {
		for (ExamBookingVo exambooking : ExamBookings) {
			EntityState entityState = EntityUtils.getState(exambooking);
			
			logger_.info(ExamBookings + " - state:" + entityState);
			if (EntityState.NEW.equals(entityState)) {
				//新增
				insertExamBooking(exambooking);
			} else if (EntityState.MODIFIED.equals(entityState)) {
				//修改
				updateExamBooking(exambooking);
			} else if (EntityState.DELETED.equals(entityState)) {
				//删除
				deleteExamBooking(exambooking.getExamBookingId());
			}
		}
	}
	/**
	 * 新增考试预约
	 * simon
	 * create at 2012-1-17 下午11:36:42
	 * <mailto:liangyuanliu@gmail.com>
	 * @param exambookingVo
	 */
	private void insertExamBooking(ExamBookingVo exambookingVo)
	{		
		
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		
		if (null != accountVo && null != accountVo.getAccoutId()) {
			exambookingVo.setCompanyId(accountVo.getCompanyId());
			exambookingVo.setCreateUser(accountVo.getAccountLoginName());
			exambookingVo.setCreateTime(new Date());
			exambookingVo.setExamCreateUser(accountVo.getAccountOwner());
			
			exambookingService.insertExamBooking(exambookingVo);
		}else{
			throw new SessionTimeOutException("登录超时...");
		}
		
		
		
	}
	/**
	 * 更新考试预约
	 * simon
	 * create at 2012-1-17 下午11:37:01
	 * <mailto:liangyuanliu@gmail.com>
	 * @param exambookingVo
	 */
	private void updateExamBooking(ExamBookingVo exambookingVo)
	{
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
		
		if (null != accountVo && null != accountVo.getAccoutId()) {
			exambookingVo.setLastModifyUser(accountVo.getAccountLoginName());
			exambookingVo.setLastModifyTime(new Date());
			
			exambookingService.updateExamBooking(exambookingVo);
		}else{
			throw new SessionTimeOutException("登录超时...");
		}
	}
	
	/**
	 * 删除考试预约
	 * simon
	 * create at 2012-1-17 下午11:37:08
	 * <mailto:liangyuanliu@gmail.com>
	 * @param id
	 */
	private void deleteExamBooking(int id)
	{
		AccountVo accountVo = DoradoSystemUtils.getCurrentUser(DoradoContext.getCurrent().getRequest());
			exambookingService.deleteExamBooking(id);
		if (null != accountVo && null != accountVo.getAccoutId()) {
			
		}else{
			throw new SessionTimeOutException("登录超时...");
		}
		
	}
	
	

}
